/**
 * 
 */
package service;

import util.JankExecutors;

import java.util.concurrent.ExecutorService;

/**
 * https://www.jianshu.com/p/931e2ea582ee
 * 写两个线程，一个线程打印1-52，另一个线程打印A-Z，打印顺序是12A34B...5152Z；
 * 
 * @author jiangnk001
 *
 */
public class MultiThreadPrintAZOrNumber {
	
	/**
	 * @param  args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		Object lock = new Object();
		
		PrintAZ printA = new PrintAZ(lock);
		PrintNumber printNumber = new PrintNumber(lock);
		
		ExecutorService executorPool = JankExecutors.newFixedSizeExecutorPool(2, "print");
		executorPool.submit(printNumber);
		executorPool.submit(printA);
		
	}
}

class PrintAZ implements Runnable{
	
	private final Object lock;
	
	private char begin = 'A';
	
	public PrintAZ(Object p){
		this.lock = p;
	}
	
	@Override
	public void run(){
		while (begin <= 'Z') {
			synchronized (lock) {
				lock.notify();
				System.out.println(begin++);
				try {
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}	
}

class PrintNumber implements Runnable{
	private Object lock;
	
	private int begin = 1;
	
	PrintNumber(Object olock){
		this.lock = olock;
	}

	@Override
	public void run() {
		while(begin < 52){
			synchronized (lock) {
				lock.notify();
				System.out.print(begin++);
				System.out.print(begin++);
				try {
					lock.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
	
	
}


